const express = require('express')
const app = express()
const path = require('path')
//启动中间健
app.use(express.json())
app.use(express.urlencoded({extended:false}))

app.post('/postData',(req,res)=>{

  const {
    name,
    sex,
    age
  }=req.body
  // console.log(req.body)
  if (!name||!sex||!age){
    return res.send({
      code:10001,
      msg:'提交数据失败',
      data:null
    })
  }

  res.send({
    code:10000,
    msg:'提交数据成功',
    data:{
      name,
      sex,
      age
    }
  })
})

app.get('/user',(req,res)=>{
  const {
    userId
  } = req.query

  if (userId !== '001'){
    return res.send({
      code:10001,
      msg:'查询数据失败',
      data:null
    })
  }

  res.send({
    code:10000,
    msg:'查询数据成功',
    data:{
      userId:'001',
      userName:'万登豪',
      userSex:'男',
      userAge:18
    }
  })

})

app.get('/getAxios',(req,res)=>{
  const {
    userId
  } = req.query

  if (userId !== '001'){
    return res.send({
      code:10001,
      message:'查询数据失败',
      data:null
    })
  }

  res.send({
    code:10000,
    message:'查询数据成功',
    data:{
      userId:'001',
      userName:'万登豪',
      userSex:'男',
      userAge:18
    }
  })
})

app.put('/putAxios',(req,res)=>{
  const {
    name,
    sex,
    age
  }=req.body
  // console.log(req.body)
  if (!name||!sex||!age){
    return res.send({
      code:10001,
      msg:'提交数据失败',
      data:null
    })
  }

  res.send({
    code:10000,
    msg:'提交数据成功',
    data:{
      name,
      sex,
      age
    }
  })
})

app.delete('/deleteAxios',(req,res)=>{
  const {
    userId
  }=req.query

  if (userId!=='001'){
    return res.send({
      code:10001,
      msg:'删除失败,对象可能不存在'
    })
  }
  res.send({
    code:10000,
    msg:'删除成功'
  })
})

app.get('/interceptor',(req,res)=>{
  const {userId} = req.query
  const token = req.headers.token
  console.log(req.headers)
  if (!token){
    return res.send({
      code:10002,
      msg:'当前个人信息不正确,请确认后访问',
      data:null
    })
  }
  if (userId!=='001'){
    return res.send({
      code:10001,
      msg:'查询用户不正确',
      data:null
    })
  }
  res.send({
    code:10000,
    msg:'查询数据成功',
    data:{
      userId:'001',
      userName:'万登豪',
      userSex:'男',
      userAge:18
    }
  })
})

app.get('/breakConnect',(req,res)=>{
  setTimeout(()=>{
    const {
      userId
    } = req.query

    if (userId !== '001'){
      return res.send({
        code:10001,
        message:'查询数据失败',
        data:null
      })
    }

    res.send({
      code:10000,
      message:'查询数据成功',
      data:{
        userId:'001',
        userName:'万登豪',
        userSex:'男',
        userAge:18
      }
    })
  },3000)
})


app.get('/1',(req,res)=>{

  const filePath = path.resolve(__dirname,'./axios基础使用.html')
  res.sendFile(filePath)

})

app.get('/2',(req,res)=>{

  const filePath = path.resolve(__dirname,'./axios请求别名.html')
  res.sendFile(filePath)

})

app.get('/3',(req,res)=>{

  const filePath = path.resolve(__dirname,'./axios请求拦截器配置.html')
  res.sendFile(filePath)

})

app.get('/4',(req,res)=>{

  const filePath = path.resolve(__dirname,'./axios取消请求.html')
  res.sendFile(filePath)

})


app.listen(8090,()=>{
  console.log('running!!! 可以访问 http://127.0.0.1:8090')
})